package com.betty.cscams.security;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.betty.cscams.common.exception.AuthCscaException;
import com.betty.cscams.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;


/**
 * 登录认证过滤器
 */
public class JwtAuthenticationFilter extends BasicAuthenticationFilter {

    @Autowired
    private HandlerExceptionResolver handlerExceptionResolver;
	@Autowired
    public JwtAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(authenticationManager);
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
    	// 获取token, 并检查登录状态
//        try{
//            SecurityUtils.checkAuthentication(request);
//        }catch (Exception e){
//            //交给全局异常处理后  结束执行  ErrorException为自定义的异常
//            handlerExceptionResolver.resolveException(request, response, null, new AuthCscaException(e.getMessage()));
//            return;
//        }
        SecurityUtils.checkAuthentication(request);
        chain.doFilter(request, response);
    }
    
}